package com.example.service.impl;

import com.example.exception.ServiceException;
import com.example.mapper.UserMapper;
import com.example.pojo.query.UserQuery;
import com.example.pojo.vo.UserVO;
import com.example.service.UserService;
import com.example.utils.ResultCodeUtil;
import com.example.utils.Sha256Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.pojo.entity.User;

import java.util.ArrayList;
import java.util.List;

@Service
class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<UserVO> selectUserList(UserQuery userQuery) {
        userQuery.setDate();
        Integer pageNo = userQuery.getPageNo();
        Integer pageSize = userQuery.getPageSize();
        Integer offset = (pageNo - 1) * pageSize;
        userQuery.setPageNo(offset);
        List<User> listUser = userMapper.selectListUser(userQuery);
        List<UserVO> userVOList = new ArrayList<>();
        listUser.forEach(user -> {
            userVOList.add(new UserVO(user));
        });

        return userVOList;
    }

    @Override
    public UserVO selectByUserName(String userName) {
        if (userName == null || userName.isEmpty()) {
            throw new ServiceException(ResultCodeUtil.DATA_ERROR, "数据错误");
        }
        User byUserName = userMapper.selectByUserName(userName);
        if (byUserName != null) {
            return new UserVO(byUserName);
        }
        return null;
    }

    @Override
    public UserVO selectByUserNameAndPasswd(User login) {
        String password = login.getPassword();
        if (password == null || password.isEmpty()) {
            throw new ServiceException(ResultCodeUtil.DATA_ERROR, "数据错误");
        }
        String encryption = Sha256Util.encryption(password);
        login.setPassword(encryption);
        User user = userMapper.selectByUserNameAndPasswd(login);
        if (user != null) {
            UserVO userVO = new UserVO(user);
            return userVO;
        }
        throw new ServiceException(ResultCodeUtil.DATA_ERROR, "用户名或密码错误");
    }

    @Override
    public void insertUser(User user) {
        user.insert();
        String password = user.getPassword();
        if (password == null || password.isEmpty()) {
            throw new ServiceException(ResultCodeUtil.DATA_ERROR, "数据错误");
        }
        String encryption = Sha256Util.encryption(password);
        user.setPassword(encryption);
        String userName = user.getUserName();
        UserVO byUserName = selectByUserName(userName);
        if (byUserName == null) {
            int insert = userMapper.insertUser(user);
            return;
        }
        throw new ServiceException(ResultCodeUtil.DATA_EXISTS, "用户名已存在");
    }

    @Override
    public void updateUser(User user) {
        updateUser(user, true);
    }

    @Override
    public void updateUser(User user, boolean updateLogin) {
        if (updateLogin) {
            user.update();
        }
        String password = user.getPassword();
        boolean updatePasswd = password != null && !password.isEmpty();
        if (updatePasswd) {
            String encryption = Sha256Util.encryption(password);
            user.setPassword(encryption);
        }

        userMapper.updateByUserName(user);
        if(updatePasswd){
            throw new ServiceException(ResultCodeUtil.NOT_LOGIN,"密码更新，重新登录");
        }
        return;
    }


}
